﻿using System;
using System.Collections.Generic;
using System.Text;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.Validation.Interfaces;

namespace Kiiro.Validation.Validators.Entities
{
    public class TeamMemberValidator : BaseEntityValidator, IValidator<TeamMember>
    {
        private readonly List<ValidationMessage> validationErrors = new List<ValidationMessage> ( );

        #region  IValidator<Setup> Members
        public List<ValidationMessage> Validate ( TeamMember entityToValidate )
        {
            return Validate ( entityToValidate, null, default ( int ) );
        }

        public List<ValidationMessage> Validate ( TeamMember entityToValidate, Project project, int assignedWorkCount )
        {
            ValidationMessage message = null;

            if ( entityToValidate.EditType == EditType.Deleted || entityToValidate.EditType == EditType.Updated )
            {
                if ( project != null
                    && project.ProjectManagerIds.Contains ( entityToValidate.UserId )
                    && project.ProjectManagerIds.Count == 1
                    && ( entityToValidate.IsProjectManager == false || entityToValidate.EditType == EditType.Deleted ) )
                {
                    message = new ValidationMessage ( ValidationSeverity.Validation, "At least one project manager must exist for the project." );
                    entityToValidate.IsProjectManagerMessage.Add ( message );
                }
            }

            if ( entityToValidate.EditType == EditType.Deleted && assignedWorkCount > 0 )
            {
                message = new ValidationMessage ( ValidationSeverity.Validation, "Cannot delete Team Members with assigned Work." );
                entityToValidate.EditTypeMessage.Add ( message );
            }

            // If the entity is marked as deleted, don't do any validation, just return.
            if ( entityToValidate.EditType != EditType.Deleted )
            {
                #region Required Fields Checks

                message = IsRequired ( entityToValidate.UserId, "User Id" );
                if ( message != null )
                {
                    entityToValidate.UserIdMessage.Add ( message );
                    validationErrors.Add ( message );
                }

                #endregion

                #region Comparison Checks

                if ( !string.IsNullOrEmpty ( entityToValidate.RoleOnProject ) )
                {
                    message = CheckMaximumLength ( 255, entityToValidate.RoleOnProject, "Role" );

                    if ( message != null )
                    {
                        entityToValidate.RoleOnProjectMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    message = HasScriptingTagsInText ( entityToValidate.RoleOnProject, "Role" );

                    if ( message != null )
                    {
                        entityToValidate.RoleOnProjectMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }

                #endregion
            }

            return validationErrors;
        }
        #endregion
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Validation/Validators/Entities/TeamMemberValidator.cs $
 * 
 * 4     10/26/09 11:08a Adamw
 * Case 332
 * 
 * 3     17/08/09 10:45a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/